var MusicDetail = function () {
};


MusicDetail.prototype.listenScoreEvent = function () {
    $("#score-btn").on("click", function (event) {
        event.preventDefault();
        var score = $("#input-score").val();
        var $this = $(this);
        var music_id = $this.attr('music-id');

        $.ajax({
            url: "/music/score/input/",
            type: 'POST',
            data: {
                score,
                music_id
            },
            success: function (result) {
                if (result['code'] === 200) {
                    alert('评价成功');
                    location.reload();
                } else {
                    alert(result['message']);
                }
            }
        })
    });
};

MusicDetail.prototype.listenAddLike = function () {
    $("#add-like-btn").on("click", function (event) {
        event.preventDefault();
        var $this = $(this);
        var music_id = $this.attr('music-id');

        $.ajax({
            url: "/music/like/add/",
            type: 'POST',
            data: {
                music_id
            },
            success: function (result) {
                if (result['code'] === 200) {
                    alert('加入成功');
                    location.reload()
                } else {
                    alert(result['message']);
                }
            }
        })
    });
};


MusicDetail.prototype.listenAddComment = function () {
    $("#add-comment-btn").on("click", function (event) {
        event.preventDefault();
        var $this = $(this);
        var music_id = $this.attr('music-id');
        var content = $('#comment-textarea').val();
        $.ajax({
            url: "/music/comment/add/",
            type: 'POST',
            data: {
                music_id,
                content
            },
            success: function (result) {
                if (result['code'] === 200) {
                    alert('发布成功');
                    location.reload()
                } else {
                    alert(result['message']);
                }
            }
        })
    });
};


MusicDetail.prototype.run = function () {
    this.listenAddComment();
    this.listenScoreEvent();
    this.listenAddLike();
};


$(function () {
    var handler = new MusicDetail();
    handler.run();
});